Ext.namespace("Ext.milan.pagejs.page_pubData");
Ext.milan.pagejs.page_pubData.userWin = null;
Ext.milan.pagejs.page_pubData.subWin = null;
Ext.milan.pagejs.page_pubData.REQUEST_ACTION = "pubData";
var selectPostionTree = null;
var subSortManageUpdateWin;
Ext.milan.pagejs.page_pubData.docLoadBeforeCallBack = function() {
};

Ext.milan.pagejs.page_pubData.docOnReadyCallback = function() {
	Ext.milan.pagejs.page_pubData.pdTree = new dhtmlXTreeObject("_pubdata_tree","100%","100%",0);
	var pdTree = Ext.milan.pagejs.page_pubData.pdTree;
	pdTree.setImagePath(Milan.skinType+"images/dhtmlx/tree/csh_vista/");
	pdTree.setDataMode("json");
	pdTree.loadJSON("pubData/tree.do");
	
	pdTree.attachEvent("onXLE",function(){
		var form = $("orgDepartmentListFormId");
		$("_pubdata_tree").removeChild($("holdinfo_pubdata"));
		if(form.deptID.value == -1){
			var firstId = pdTree.getItemIdByIndex(0,0);
			var firstDeptId = pdTree.getUserData(firstId,"id");
			pdTree.selectItem("PD"+firstDeptId,true);
			pdTree.openItem("PD"+firstDeptId);
		}else{
			pdTree.selectItem("PD"+form.deptID.value,true);
			pdTree.openItem("PD"+form.deptID.value);
		}
	});
	pdTree.attachEvent("onClick", function(treeItemID,previouslyItemID){   
		var form = $("orgDepartmentListFormId");
		
		$("dptName").innerHTML = pdTree.getItemText(treeItemID);
		$("parentDptName").innerHTML = pdTree.getItemText(pdTree.getParentId(treeItemID));
		$("dptSortNO").innerHTML = pdTree.getUserData(treeItemID,"sortNO");
		$("dptMemo").innerHTML = pdTree.getUserData(treeItemID,"memo");
		
		var tmpID = treeItemID;
		var deptPath = "";
		while (tmpID!="0")
		{
			deptPath = pdTree.getItemText(tmpID)+"-"+deptPath;
			tmpID = pdTree.getParentId(tmpID);
		}
		deptPath = deptPath.substring(0,deptPath.length-1);
		$("dptPath").innerHTML = deptPath;
		form.deptID.value= pdTree.getUserData(treeItemID,"id");
		form.deptName.value = pdTree.getItemText(treeItemID);
		form.parentDeptID.value = pdTree.getUserData(pdTree.getParentId(treeItemID),"id");
		form.parentDeptID1.value = pdTree.getUserData(pdTree.getParentId(treeItemID),"id");
		
			$("btnAdd2").style.display = "";
			$("btnEdit").style.display = "";
		
		if (!pdTree.getParentId(treeItemID) || pdTree.getUserData(pdTree.getParentId(treeItemID),"id")==0)
		{
			$("btnCopy").style.display = "none";
			$("btnMove").style.display = "none";
		}
		else
		{
			$("btnAdds").style.display = "";
			$("btnCopy").style.display = "";
			$("btnMove").style.display = "";
			
		}
	});
};

/**
 * 关闭子弹出窗口
 */
Ext.milan.pagejs.page_pubData.closeSubWin = function() {
	Milan.commonutils.closeModelPop(Ext.milan.pagejs.page_pubData.subWin);
};

/**
 * 关闭弹出窗口
 */
Ext.milan.pagejs.page_pubData.closeWin = function(winName) {
	if(winName){
		if(winName=='inactivateConsignerWin')
			inactivateConsignerWin.closeDialog();
	}
	else
		Milan.commonutils.closeModelPop(Ext.milan.pagejs.page_pubData.userWin);
};

/*------------------------ private methods -----------------------*/

/**
 * 查询所有
 */
Ext.milan.pagejs.page_pubData._search = function() {
	Ext.milan.pagejs.common_utils.query(false, "pubData/list.do");
};

Ext.milan.pagejs.page_pubData._formCheck = function(frm) {
	function focusElement(el) {
		if(el.focus)
			el.focus();
	};
	return true;
};

/**
 * 保存信息
 */
Ext.milan.pagejs.page_pubData._saveInfo = function(formId) {
	if(!Ext.milan.pagejs.page_pubData._formCheck($(formId)))
		return;
		
	Milan.ajaxutility.formSubmit({
		form:formId,
		success:function(response)
		{
			Ext.milan.pagejs.page_pubData.closeWin();
			Ext.milan.pagejs.page_pubData.query();
		}
	});
};

/**
 * 条件查询列表
 */
Ext.milan.pagejs.page_pubData.query = function() {
	Milan.pagejs.page_index.updateCurrentTabByForm("query_pubData_list_form");
};

/**
 * 显示新建表单
 */
Ext.milan.pagejs.page_pubData.showAddForm = function() {
	var url = Ext.milan.pagejs.page_pubData.REQUEST_ACTION + "/create.do";
	Ext.milan.pagejs.page_pubData.userWin = Milan.commonutils.createModelPop({
		title:"新建", 
		url:url,
		width:650
	});
};

/**
 * 显示编辑表单
 * 
 * @param ID
 */
Ext.milan.pagejs.page_pubData.showEditForm = function(id) {
	var url = Ext.milan.pagejs.page_pubData.REQUEST_ACTION + "/edit.do?id=" + id;
	Ext.milan.pagejs.page_pubData.userWin = Milan.commonutils.createModelPop({
		title:"编辑",
		url:url,
		width:650
	});
};

/**
 * 保存新建用户信息
 */
Ext.milan.pagejs.page_pubData.save = function(formId) {
	Ext.milan.pagejs.page_pubData._saveInfo(formId);
};

/**
 * 保存编辑后的信息
 */
Ext.milan.pagejs.page_pubData.edit = function(formId) {
	Ext.milan.pagejs.page_pubData._saveInfo(formId);
};

Ext.milan.pagejs.page_pubData.deleteForm = function(id) {
	var url = Ext.milan.pagejs.page_pubData.REQUEST_ACTION + "/delete.do?id=" + id;
	new Milan.widget.ConfirmDialog("确认信息", "确认要删除该信息吗？", {
		confirmCallback: function() {
			Milan.ajaxutility.formSubmit({
				url: url,
				method: 'post',
				success: function(response, option)
				{
					Ext.milan.pagejs.page_pubData.query();
				}
			});
		}
	});	
};

/**
 * 查看详情
 * 
 * @param entityId 用户ID
 */
Ext.milan.pagejs.page_pubData.viewDetail = function(entityId) {
	var url = Ext.milan.pagejs.page_pubData.REQUEST_ACTION + "/show.do?id=" + entityId;
	Ext.milan.pagejs.page_pubData.userWin = Milan.commonutils.createModelPop({
		title:"详细信息",
		url:url,
		width:500
	});
};

/**
 * 显示分类管理页面
 */
Ext.milan.pagejs.page_pubData.showSortManagePage = function(sortid)
{
    var url = "pubData/subList.do?pid=" + sortid;
    win = Milan.commonutils.createModelPop({
		title: "分类管理",
		height: "400px",
		width : "650px"
	});
	Milan.ajaxutility.updateContentUseAjax("post",url,win);
}

/**
 * 提交新建子分类
 */
Ext.milan.pagejs.page_pubData.addSubSort = function(e)
{
	if(e){
		e= e||event;
		if(e.keyCode != 13)return;
	}
    var form = $("query_pubData_sub_list_form");
	if(form.name.value.trim() == "")
	{
	    Milan.commonutils.createDelayNotifyModel("输入提示", "分类名称不能为空！");
		return false;
	}
	if(!Milan.string.isValidText(form.name.value.trim(), "!@#$%^&*()+|-=\\,/?':\"", true)){
		Milan.commonutils.createDelayNotifyModel("输入提示", "分类名称不能为特殊字符！");
		return false;
	}
	form.name.value=form.name.value.trim();
	form.action="pubData/save.do";
	Milan.ajaxutility.formSubmit(
    {
        form: "query_pubData_sub_list_form",
        success: function(){
        	var url = "pubData/subList.do?pid=" + form.pid.value.trim();
			Milan.ajaxutility.updateContentUseAjax("post",url, "subSortManageAdd_divId",{displayMasker: false,displayLoading: false});
		}
    });
}

Ext.milan.pagejs.page_pubData.updateSubSort = function()
{
    var form = $("subSortManageUpdateFormId");
	if(form.name.value.trim() == "")
	{
	    Milan.commonutils.createDelayNotifyModel("输入提示", "分类名称不能为空！");
		return false;
	}
	if(!Milan.string.isValidText(form.name.value.trim(), "!@#$%^&*()+|-=\\,/?':\"", true)){
		Milan.commonutils.createDelayNotifyModel("输入提示", "分类名称不能为特殊字符！");
		return false;
	}
	form.name.value=form.name.value.trim();
	Milan.ajaxutility.formSubmit(
    {
        form: "subSortManageUpdateFormId",
        success: function() {
        	if(subSortManageUpdateWin)
	    		subSortManageUpdateWin.closeDialog();
        	var url = "pubData/subList.do?pid=" + form.pid.value.trim();
        	Milan.ajaxutility.updateContentUseAjax("post",url,"subSortManageAdd_divId",{displayMasker: false,displayLoading: false});
		}
    });
}

/**
 * 删除子分类
 */
Ext.milan.pagejs.page_pubData.deleteSubSort = function(subsortid)
{
	var form = $("query_pubData_sub_list_form");
    new Milan.widget.ConfirmDialog("确认信息", "确认要删除该分类吗?", 
    {
    	confirmCallback : function(){
			Milan.ajaxutility.formSubmit(
            {				
                url: "pubData/delete.do",
				params: "id=" + subsortid,
                success: function(){
                	var url = "pubData/subList.do?pid=" + form.pid.value;
                	Milan.ajaxutility.updateContentUseAjax("post",url,"subSortManageAdd_divId",{displayMasker: false,displayLoading: false});
				}
            });
		},
		cancelCallback : function(){}
    });	
}

Ext.milan.pagejs.page_pubData.showUpdateSubSortPage = function(subsortid)
{
    var url = "pubData/editSub.do?id=" + subsortid;
    subSortManageUpdateWin = Milan.commonutils.createModelPop({
		title: "编辑分类",
		height: "400px",
		width : "450px"
	});
	Milan.ajaxutility.updateContentUseAjax("post",url,subSortManageUpdateWin);
}

Ext.milan.pagejs.page_pubData.hideWin = function()
{
	if(win)
	    win.closeDialog();
}

Ext.milan.pagejs.page_pubData.hideMyWin = function()
{
	if(subSortManageUpdateWin)
	    subSortManageUpdateWin.closeDialog();
}